import { setUserInfo, getUserInfo, isAccountExist, addAccount } from '@/service/api.js'
class funUtils {
	
	_getUserInfo() {
		getUserInfo().then(({data}) => {
			uni.setStorageSync("userInfo", data);
		})
	}
	
	//修改用户信息接口
	setUserInfoSuccss(key, num) {
		const userInfo = uni.getStorageSync("userInfo");
		setUserInfo({
			id: userInfo.id,
			[key]: Number(num)
		}).then(res => {
			if(res.status == 200) {
				uni.showToast({
					icon: 'success',
					title: "修改成功",
					duration: 2000
				})
				this._getUserInfo()
			} else {
				uni.showToast({
					icon: 'error',
					title: res.message,
					duration: 2000
				})
			}
		})
	}
	
	//修改用户头像与昵称
	setUserAvatarAndNickname(payload) {
		setUserInfo(payload).then(res => {
			if(res.status == 200) {
				uni.showToast({
					icon: "success",
					duration: 2000,
					title: "修改成功",
					success: () => {
						uni.switchTab({
							url: "/pages/profile/profile",
							success: () => {
								this._getUserInfo();
								let page = getCurrentPages().pop();
								if (page == undefined || page == null) return;
								page.onLoad();
							}
						})
						
					}
				})
			} else {
				uni.showToast({
					icon: "error",
					duration: 2000,
					title: res.message,
				})
			}
		})
	}
	
	
	
	//判断是否存在
	_isAccountExist(phone) {
		const { data } = isAccountExist(phone);
		if(data == 1) {
			//账号存在
			return true;
		} else if (data == 0) {
			//账号不存在
			const params = {
				password: '123456',
				phone,
				nickname: "",
				username: phone,
				headUrl: ""
			}
			addAccount(params).then(res => {
				if(res.status == 200) {
					return true;
				}
			})
		} else {
			return false
		}
	}
	
	
	
}


export default new funUtils();